package com.wenhao.tencent;

import java.util.LinkedList;
import java.util.Scanner;

/**
 * @author Wenhao Tong
 * @create 2021-08-22 21:36
 */
public class Main5 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] a = new int[n];
        int preHigh = a[0];
        int high = a[0];
        int low = a[0];
        int preLow = a[0];
        int sum = 0;
        for (int i = 1;i < n;i++) {
            if (a[i] < a[i - 1]) {
                preHigh = high;
                high = a[i];
            }
            if (a[i] > a[i - 1]) {
                preLow = low;
                low = a[i];
            }
            sum += high - preHigh;
        }
        System.out.println(preHigh + sum);
    }
}
